package com.ccl.iot.batch;

import com.ccl.iot.App;
import com.ccl.iot.bean.BatchBean;
import com.ccl.iot.bean.BatchBeanDao;

import java.util.List;

/**
 * 类说明：
 * 作者：hehandong on 2017/12/21 11:28
 * 邮箱：hehandong@qq.com
 */

public class BatchDBManager {

    public static List<BatchBean> queryDataList(long batchID) {
        BatchBeanDao batchBeanDao = App.getBatchBeanDao();
        if (batchBeanDao != null){
            return batchBeanDao.queryBuilder().where(BatchBeanDao.Properties.BatchID.eq(batchID)).list();
        }
        return null;
    }



    public static BatchBean getBatchBean(long batchID) {
        List<BatchBean> batchBeans = queryDataList(batchID);
        if (batchBeans != null && batchBeans.size() > 0) {
            return batchBeans.get(0);
        }
        return null;
    }

    public static List<BatchBean> queryAllData() {
        BatchBeanDao batchBeanDao = App.getBatchBeanDao();
        if (batchBeanDao != null){
            return batchBeanDao.queryBuilder().list();
        }
        return null;
    }

    public static void saveBatch(long batchID, String nameAndImgIndex, String iotBatchItemInfoListJson) {

        BatchBean batchBean = getBatchBean(batchID);

        if (batchBean == null) {
            batchBean = new BatchBean();
            batchBean.setBatchID(batchID);
            batchBean.setNameAndImgIndex(nameAndImgIndex);
            batchBean.setIotBatchItemInfoListJson(iotBatchItemInfoListJson);
            App.getBatchBeanDao().insert(batchBean);
        } else {
            String nameImgIndex = batchBean.getNameAndImgIndex();
            String infoListJson = batchBean.getIotBatchItemInfoListJson();
            if (!nameImgIndex.equals(nameAndImgIndex) && !infoListJson.equals(iotBatchItemInfoListJson)){
                batchBean.setNameAndImgIndex(nameAndImgIndex);
                batchBean.setIotBatchItemInfoListJson(iotBatchItemInfoListJson);
                App.getBatchBeanDao().update(batchBean);
            }
        }
    }

}
